package com.kymatrix.mpo.plus.bpm.server.controller.definition;

import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmCategoryEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.BpmCategoryResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.ListQueryBpmCategoryResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.PageQueryBpmCategoryReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.PageQueryBpmCategoryResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.SaveBpmCategoryReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.UpdateBpmCategoryReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmCategoryService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.business.common.ResultResp;
import com.kymatrix.mpo.plus.common.enums.EnableTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "BPM 流程分类相关请求处理类")
@RestController
@Validated
public class BpmCategoryController {

    @Resource
    private BpmCategoryService categoryService;

    @PostMapping("/bpm/category/create")
    @ApiOperation(value = "创建流程分类")
    public ResultResp<Long> createCategory(@Valid @RequestBody SaveBpmCategoryReq createReqVO) {
        return ResultResp.ok(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/bpm/category/update")
    @ApiOperation(value = "更新流程分类")
    public ResultResp<Boolean> updateCategory(@Valid @RequestBody UpdateBpmCategoryReq updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return ResultResp.ok(true);
    }

    @DeleteMapping("/bpm/category/delete")
    @ApiOperation(value = "删除流程分类")
    public ResultResp<Boolean> deleteCategory(@RequestParam("id") String id) {
        categoryService.deleteCategory(id);
        return ResultResp.ok(true);
    }

    @GetMapping("/bpm/category/get")
    @ApiOperation(value = "获得流程分类")
    public ResultResp<BpmCategoryResp> getCategory(@RequestParam("id") String id) {
        BpmCategoryEntity category = categoryService.getCategory(id);
        BpmCategoryResp resp = new BpmCategoryResp();
        resp.setId(category.getId());
        resp.setName(category.getName());
        resp.setCode(category.getCode());
        resp.setDescription(category.getDescription());
        resp.setStatus(category.getStatus());
        resp.setSort(category.getSort());
        resp.setCreateTime(category.getCreateTime());
        return ResultResp.ok(resp);
    }

    @GetMapping("/bpm/category/page")
    @ApiOperation(value = "获得流程分类分页")
    public ResultResp<PageResp<PageQueryBpmCategoryResp>> getCategoryPage(@Valid PageQueryBpmCategoryReq pageReqVO) {
        return ResultResp.ok( categoryService.getCategoryPage(pageReqVO));
    }

    @GetMapping("/bpm/category/simple-list")
    @ApiOperation(value = "获取流程分类的精简信息列表", notes = "只包含被开启的分类，主要用于前端的下拉选项")
    public ResultResp<List<ListQueryBpmCategoryResp>> getCategorySimpleList() {
        List<BpmCategoryEntity> list = categoryService.getCategoryListByStatus(EnableTypeEnum.ENABLE.getCode());
        list.sort(Comparator.comparingInt(BpmCategoryEntity::getSort));
        List<ListQueryBpmCategoryResp> resp = new ArrayList<>();
        for (BpmCategoryEntity entity : list) {
            resp.add(ListQueryBpmCategoryResp.formEntity(entity));
        }
        return ResultResp.ok(resp);
    }

}
